<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class ApiController extends Controller
{



    public function __construct()
    {
        //TODO:验证签名
        if (config('app.sign') == true) {
            $error = headerValidation();
            if($error){
                //Log::info(__METHOD__ . ' - sign error :' . json_encode(request()->header(), JSON_UNESCAPED_UNICODE));
                $info = $this->json([],202,$error);
                exit(json_encode($info->getData(),JSON_UNESCAPED_UNICODE));
            }
        }
    }
    /**
     * 用于返回结果(封装)
     * @param string $data 返回数据
     * @param int $code 返回错误码
     * @param string $err 返回错误消息
     * @return \Illuminate\Http\JsonResponse
     */
    public function json($data='', $code=0, $err=''){
        if(0 != $code){
            $ret = array(
                'code'   => $code,
                'msg'    => empty($err) ? trans("error.err_{$code}") : $err ,
                'result' => $data,
            );
        }else{
            $ret = array(
                'code'   => $code,
                'msg'    => $err ?? '',
                'result' => $data,
            );
        }
        return response()->json($ret);
    }
}
